package inventory.controller;

import inventory.common.ResultData;
import inventory.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static inventory.common.CommonConstants.ROLE_PREFIX;

/**
 * @author rhq
 * @create 2019-11-12 20:05
 */
@Api(tags = "角色相关")
@RestController
@RequestMapping(ROLE_PREFIX)
public class RoleController {

    @Autowired
    private RoleService roleService;


//    @RequestMapping(value = "/getRoleOfLowLevalRole/{roleId}",method = RequestMethod.GET)
//    public ResultData getRoleOfLowLevalRole(String roleId){
//
//        List<Role> countyRoles = roleService.selectList(new EntityWrapper<Role>().eq("pid",roleId));
//
//        return new ResultData().OK().setObjs(countyRoles);
//    }

    //-------------------------------Rhq--------------------start-----------------

    /**
     * 通过managerId查询下级角色
     *
     * @param managerId
     * @return
     */

    @ApiOperation(value = "通过managerId查询下级角色")
    @PostMapping(value = "/getSubRolesByManagerId", params = {"managerId"})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "managerId", value = "管理员Id")
    )
    @RequestMapping(value = "/getSubRolesByManagerId", method = RequestMethod.GET)
    public ResultData getSubRolesByManagerId(String managerId) {

        ResultData resultData = roleService.getSubRolesByManagerId(managerId);

        return resultData;
    }

    /**
     *  通过市级roleId增加县区及员工 同时增加县区仓库
     * @return
     */
    @RequiresPermissions("role:operate")
    @ApiOperation(value = "通过市级roleId增加县区及员工 同时增加县区仓库")
    @PostMapping(value = "/addSubCountyRoleAndSubCountyRepositoryByCityRoleId", params = {"cityRoleId", "role"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityRoleId", value = "市级roleId"),
            @ApiImplicitParam(name = "role", value = "增加的role,即区县名")
    })
    @RequestMapping(value = "/addSubCountyRoleAndSubCountyRepositoryByCityRoleId", method = RequestMethod.POST)
    public ResultData addSubCountyRoleAndSubCountyRepositoryByCityRoleId(String cityRoleId, String role) {

        ResultData resultData = roleService.addSubCountyRoleAndSubCountyRepositoryByCityRoleId(cityRoleId, role);

        return resultData;
    }


    /**
     * 通过县区roleId删除角色同时删除仓库
     * <p>
     * 先通过判定区县的role_id到role表找到下级role_id，然后到user表比对是否还有底层员工
     * 有则不能删除角色和库，没有就删repository中的区县库 还有role表中区县role以及下级role
     *
     * @param countyRoleId
     * @return
     */
    @RequiresPermissions("role:operate")
    @ApiOperation(value = "通过县区roleId删除角色同时删除仓库", notes = "先通过判定区县的role_id到role表找到下级role_id，然后到user表比对是否还有底层员工\n" +
            "有则不能删除角色和库，没有就删repository中的区县库 还有role表中区县role以及下级role")
    @PostMapping(value = "/deleteCountyRoleAndCountyRepositoryByCountyRoleId", params = {"countyRoleId"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countyRoleId", value = "区县roleId"),
    })
    @RequestMapping(value = "/deleteCountyRoleAndCountyRepositoryByCountyRoleId", method = RequestMethod.POST)
    public ResultData deleteCountyRoleAndCountyRepositoryByCountyRoleId(String countyRoleId) {

        ResultData resultData = roleService.deleteCountyRoleAndCountyRepositoryByCountyRoleId(countyRoleId);

        return resultData;
    }

    /**
     * 通过县区roleId更改role
     *
     * @param countyRoleId
     * @param role
     * @return
     */
    @RequiresPermissions("role:operate")
    @ApiOperation(value = "通过县区roleId更改role")
    @PostMapping(value = "/updateRoleByCountyRoleId", params = {"countyRoleId"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countyRoleId", value = "区县roleId"),
            @ApiImplicitParam(name = "role", value = "需要更改的名字,即区县名")
    })
    @RequestMapping(value = "/updateRoleByCountyRoleId", method = RequestMethod.POST)
    public ResultData updateRoleByCountyRoleId(String countyRoleId, String role) {

        ResultData resultData = roleService.updateRoleByCountyRoleId(countyRoleId, role);

        return resultData;
    }

    //-------------------------------Rhq--------------------end-----------------


}
